package autoteam.tables;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import autoteam.League;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class LeagueTableModel extends AutoTeamTableModel
{
    private static final long serialVersionUID = 5835956173837870322L;
    
    private League currentLeague;
    
    private String[] columnNames = { "League","Sport" };
    private List<DBRecord> leagueRecords;
    private int rowCount;
    private Map<Integer,String> colMap;

	private DataAccessObject dao;
    
    public LeagueTableModel( )
    {
        super( );
    }
    
    public LeagueTableModel( League newLeague )
    {

		currentLeague = newLeague;
		dao = new DataAccessObject("rsdb");
		leagueRecords = dao.getAllRecords("leagues");
		rowCount = leagueRecords.size();
		colMap = new HashMap<Integer,String>();
		colMap.put(0, "leagueName");
		colMap.put(1, "sport");
    }
    
    public void setCurrentLeague(League currentLeague)
    {
    	this.currentLeague = currentLeague;
    }

    public League getCurrentLeague()
    {
    	return currentLeague;
    }

    @Override
    public void add( int j )
    {
    	//TODO
    	rowCount++;
    	fireTableDataChanged();
	
    }
    
    @Override
    public void delete( int r )
    {
    	// TODO Auto-generated method stub
    	ColumnData leagueToDelete = new ColumnData("leagueName",getValueAt(r,0));
    	dao.deleteRecord("leagues", leagueToDelete);
    	rowCount--;
    	fireTableDataChanged();
    }

    @ Override
    public int getColumnCount( )
    {
    	return 2;
    }

    public String getColumnName(int col)
    {
    	return columnNames[col];
    }
    
    @ Override
    public int getRowCount( )
    {
		return rowCount;
    }
    
    @ Override
    public Object getValueAt( int rowIndex, int columnIndex )
    {
    	String colName = colMap.get(columnIndex);
    	leagueRecords = dao.getAllRecords("leagues");
    	return leagueRecords.get(rowIndex).getFieldValue(colName);
    }
}
